// echo partial file, should not compile along

using System;
using System.Reflection;
using System.Reflection.Emit;
using System.Collections.Generic;
using HarmonyLib;
using Utils;
namespace GongfaEffect;

using GameData.Domains;

public partial class GongfaEffect {
//! 我终于开始对战斗功法下毒手了
    public static class TaiZuChangQuan {
        public static short Defkey_LuanQingZhang=723; // 目前这个方法并没有被暴露在defkey之外，因此只能如此使用。
        public static short RevEff_ShiXiangTieDanShou=1050;
        public static void Patch(RobustHarmonyInstance HarmonyInstance, string ModIdStr){
            HarmonyInstance.PatchAll(System.Reflection.MethodBase.GetCurrentMethod().ReflectedType);
            // for(int i=0;i<Config.SpecialEffect.Count;i++){
            //     if(Config.SpecialEffect.Instance[i].Name=="太祖长拳"){
            //         ((List<Config.SpecialEffectItem>)typeof(Config.SpecialEffect).GetField("_dataArray",(BindingFlags)(-1)).GetValue(Config.SpecialEffect.Instance))[i].ShortDesc="送你去见祖宗";
            //     }
            // }
        }
#if ANCIENT_IMPL_FAILED
        //! 这样似乎是不成立的
        //! 由于我们不能扩展太祖长拳的OnCombatEnd类，这里使用MetAncestor类来保存__instance信息。
        public class MetAncestor {
            public GameData.Domains.SpecialEffect.CombatSkill.NoSect.FistAndPalm.TaiZuChangQuan skill;
            public GameData.DomainEvents.Events.OnCombatBegin ocb;
            public GameData.DomainEvents.Events.OnCombatEnd   oce;
            public MetAncestor(GameData.Domains.SpecialEffect.CombatSkill.NoSect.FistAndPalm.TaiZuChangQuan skill){
                this.skill=skill;
                this.ocb=new GameData.DomainEvents.Events.OnCombatBegin(this.OnCombatBegin);
                this.oce=new GameData.DomainEvents.Events.OnCombatEnd  (this.OnCombatEnd  );
                GameData.Utilities.AdaptableLog.Info("MetAncestor..ctor-执行完毕");
            }
            private void OnCombatBegin(GameData.Common.DataContext context) {
                GameData.Utilities.AdaptableLog.Info("MetAncestor.OnCombatBegin-开始执行");
                for (int i = 0; i < 9; i++)
                {
                    DomainManager.Combat.AddTrick(context, GameData.Domains.DomainManager.Combat.GetElement_CombatCharacterDict(this.skill.CharacterId), 6, true, true);
                }
                DomainManager.Combat.ShowSpecialEffectTips(context, this.skill.CharacterId, RevEff_ShiXiangTieDanShou, 0);
            }
            private void OnCombatEnd(GameData.Common.DataContext context) {
                GameData.Utilities.AdaptableLog.Info("MetAncestor.OnCombatEnd-开始执行");
                ProcessMetAncestor(this.skill, context);
                GameData.DomainEvents.Events.UnRegisterHandler_CombatBegin(this.ocb);
                GameData.DomainEvents.Events.UnRegisterHandler_CombatEnd(this.oce);
            }
        }
        [HarmonyPatch(typeof(GameData.Domains.SpecialEffect.CombatSkill.NoSect.FistAndPalm.TaiZuChangQuan),"OnEnable")]
        public static void Postfix(GameData.Domains.SpecialEffect.CombatSkill.NoSect.FistAndPalm.TaiZuChangQuan __instance) {
            GameData.Utilities.AdaptableLog.Info("TaiZuChangQuan.OnEnable-开始执行");
            var ma=new MetAncestor(__instance);
            GameData.DomainEvents.Events.RegisterHandler_CombatBegin(ma.ocb);
            GameData.DomainEvents.Events.RegisterHandler_CombatEnd  (ma.oce);
        }

        //[HarmonyPostfix]
        //[HarmonyPatch(typeof(GameData.Domains.SpecialEffect.CombatSkill.NoSect.FistAndPalm.TaiZuChangQuan),"OnCastSkillEnd")]
        //public static unsafe void Postfix(GameData.Domains.SpecialEffect.CombatSkill.NoSect.FistAndPalm.TaiZuChangQuan __instance,  int charId, short skillId, GameData.Common.DataContext context) {
        //! OnCastSkillEnd是技能结束时触发，而OnCombatSettlement必须注册特效之后才能触发，如果不使用技能，结算时不会触发。
#elif OLD_IMPL
        [HarmonyPostfix]
        [HarmonyPatch(typeof(GameData.Domains.SpecialEffect.CombatSkill.NoSect.FistAndPalm.TaiZuChangQuan),"OnCombatSettlement")]
        public static unsafe void OnCombatSettlement(GameData.Domains.SpecialEffect.CombatSkill.NoSect.FistAndPalm.TaiZuChangQuan __instance, GameData.Common.DataContext context, sbyte combatStatus) {
           if(combatStatus == GameData.Domains.Combat.CombatStatusType.EnemyFail){
               ProcessMetAncestor(__instance, context);
           }
        }
        [HarmonyPostfix]
        [HarmonyPatch(typeof(GameData.Domains.SpecialEffect.CombatSkill.NoSect.FistAndPalm.TaiZuChangQuan),"OnCastSkillEnd")]
        public static unsafe void OnCastSkillEnd(GameData.Domains.SpecialEffect.CombatSkill.NoSect.FistAndPalm.TaiZuChangQuan __instance, GameData.Common.DataContext context, int charId, bool isAlly, short skillId, sbyte power, bool interrupted) {
            if(charId == __instance.CharacterId && skillId == __instance.SkillKey.SkillTemplateId) {
                for (int i = 0; i < 2; i++) {
                    DomainManager.Combat.AddTrick(context, GameData.Domains.DomainManager.Combat.GetElement_CombatCharacterDict(__instance.CharacterId), 6, true, true);
                }
                DomainManager.Combat.ShowSpecialEffectTips(context, __instance.CharacterId, RevEff_ShiXiangTieDanShou, 0);
            }
            var combatChar = GameData.Domains.DomainManager.Combat.GetElement_CombatCharacterDict(__instance.CharacterId);
            DomainManager.Combat.ChangeStanceValue(combatChar, 3000, context, false);
            DomainManager.Combat.ChangeBreathValue(combatChar, 3000, context, false);
        }
        public static GameData.Domains.Character.PoisonInts Poison=new GameData.Domains.Character.PoisonInts(32000,32000,32000,32000,32000,32000);
        public static GameData.Domains.Character.Injuries Injuries=new GameData.Domains.Character.Injuries(6,6,6,6,6,6,6,6,6,6,6,6,6,6);
        public static unsafe void ProcessMetAncestor(GameData.Domains.SpecialEffect.CombatSkill.NoSect.FistAndPalm.TaiZuChangQuan __instance, GameData.Common.DataContext context) {
            // int charId = __instance.CharacterId;
            // short skillId = __instance.SkillKey.SkillTemplateId;
            // sbyte combatStatus = GameData.Domains.Combat.CombatStatusType.EnemyFail;
            var combatChar = GameData.Domains.DomainManager.Combat.GetElement_CombatCharacterDict(__instance.CharacterId);
            var enemyChar = GameData.Domains.DomainManager.Combat.GetMainCharacter(!combatChar.IsAlly);
            if(GameData.Domains.DomainManager.Combat.IsCharacterFallen(enemyChar)){
                var cchar=GameData.Domains.DomainManager.Combat.GetCombatCharacter(!combatChar.IsAlly, true);
                //var character=cchar.GetCharacter();
                var poison=Poison;
                cchar.SetPoison(ref poison, context);
                cchar.SetOldPoison(ref poison, context);
                cchar.SetInjuries(Injuries, context);
                cchar.SetOldInjuries(Injuries, context);
                cchar.SetOldDisorderOfQi(8000,context);
                cchar.GetCharacter().SetDisorderOfQi(8000,context);
            }
        }
#else
        public class MetAncestor {
            // public GameData.Domains.SpecialEffect.CombatSkill.NoSect.FistAndPalm.TaiZuChangQuan skill;
            public int CharacterId;
            public short SkillTemplateId;
            public bool IsDirect;
            public MetAncestor(GameData.Domains.SpecialEffect.CombatSkill.NoSect.FistAndPalm.TaiZuChangQuan skill, bool IsDirect){
                this.CharacterId=skill.CharacterId;
                this.SkillTemplateId=skill.SkillKey.SkillTemplateId;
                this.IsDirect=IsDirect;
            }
            public void Init() {
                GameData.DomainEvents.Events.RegisterHandler_PrepareSkillBegin(this.opsb);
                GameData.DomainEvents.Events.RegisterHandler_CombatCharFallen(this.occf);
                GameData.DomainEvents.Events.RegisterHandler_CastSkillEnd(this.ocse);
                // GameData.Utilities.AdaptableLog.Info("MetAncestor.Init("+this.CharacterId+")-执行完毕");
            }
            public void ocse(GameData.Common.DataContext context, int charId, bool isAlly, short skillId, sbyte power, bool interrupted) {
                if(charId == this.CharacterId){
                    GameData.DomainEvents.Events.UnRegisterHandler_PrepareSkillBegin(this.opsb);
                    GameData.DomainEvents.Events.UnRegisterHandler_CombatCharFallen(this.occf);
                    GameData.DomainEvents.Events.UnRegisterHandler_CastSkillEnd(this.ocse);
                    // GameData.Utilities.AdaptableLog.Info("MetAncestor.ocse("+this.CharacterId+")-执行完毕");
                }
            }
            public void opsb(GameData.Common.DataContext context, int charId, bool isAlly, short skillId){
                if(charId == this.CharacterId && skillId == this.SkillTemplateId) {
                    // GameData.Utilities.AdaptableLog.Info("MetAncestor.opsb("+this.CharacterId+")-成功触发");
                    for (int i = 0; i < 2; i++) {
                        DomainManager.Combat.AddTrick(context, GameData.Domains.DomainManager.Combat.GetElement_CombatCharacterDict(this.CharacterId), 6, true, true);
                    }
                    DomainManager.Combat.ShowSpecialEffectTips(context, this.CharacterId, RevEff_ShiXiangTieDanShou, 0);
                    var combatChar = GameData.Domains.DomainManager.Combat.GetElement_CombatCharacterDict(this.CharacterId);
                    DomainManager.Combat.ChangeStanceValue(combatChar, 3000, context, false);
                    DomainManager.Combat.ChangeBreathValue(combatChar, 3000, context, false);
                }
            }
            public static GameData.Domains.Character.PoisonInts Poison=new GameData.Domains.Character.PoisonInts(32000,32000,32000,32000,32000,32000);
            public static GameData.Domains.Character.Injuries Injuries=new GameData.Domains.Character.Injuries(6,6,6,6,6,6,6,6,6,6,6,6,6,6);
            public void occf(GameData.Common.DataContext context, GameData.Domains.Combat.CombatCharacter combatChar){
                if(combatChar.GetId() != this.CharacterId) {
                    // GameData.Utilities.AdaptableLog.Info("MetAncestor.occf("+this.CharacterId+")-成功触发");
                    // DomainManager.Combat.AppendFatalDamageMark(context, combatChar, 127);
                    GameData.Domains.Combat.DefeatMarkCollection markCollection = combatChar.GetDefeatMarkCollection();
                    markCollection.FatalDamageMarkCount = 127;
                    combatChar.SetDefeatMarkCollection(markCollection, context);
                    var poison=Poison;
                    combatChar.SetPoison(ref poison, context);
                    combatChar.SetOldPoison(ref poison, context);
                    combatChar.SetInjuries(Injuries, context);
                    combatChar.SetOldInjuries(Injuries, context);
                    combatChar.SetOldDisorderOfQi(8000,context);
                    combatChar.GetCharacter().SetDisorderOfQi(8000,context);
                    // DomainManager.Combat.UpdateBodyDefeatMark(combatChar, context, false);
                    var character = GameData.Domains.DomainManager.Combat.GetElement_CombatCharacterDict(this.CharacterId).GetCharacter();
                    // var fameActionRecords = character.GetFameActionRecords();
                    // HashSet<short> set = new List<short>();
                    // foreach (var fameActionRecord in fameActionRecords) {
                    //     set.Add(fameActionRecord.Id);
                    // }
                    // foreach (short num2 in set) {
                    //     var fameActionItem = FameAction.Instance[num2];
                    //     int reductionTime = (int)fameActionItem.ReductionTime;
                    //     EventHelper.ChangeFameActionDuration(character2, num2, ((fameActionItem.Fame<0) ^ this.IsDirect) ? -reductionTime : reductionTime);
                    // }

                    int currDate = DomainManager.World.GetCurrDate();
                    var fameActionRecords = character.GetFameActionRecords();
                    for (int index = fameActionRecords.Count - 1; index >= 0; index--) {
                        var fameRecord = fameActionRecords[index];
                        var fameActionItem = Config.FameAction.Instance[fameRecord.Id];
                        var reductionTime = (int)fameActionItem.ReductionTime;
                        fameRecord.EndDate = currDate + Math.Min(fameRecord.EndDate + (((fameActionItem.Fame>0) ^ this.IsDirect) ? -reductionTime : reductionTime) - currDate,  12000);
                        if (fameRecord.EndDate < currDate) {
                            GameData.Utilities.CollectionUtils.SwapAndRemove(fameActionRecords, index);
                        } else {
                            fameActionRecords[index] = fameRecord;
                        }
                    }
                    character.SetFameActionRecords(fameActionRecords, context);
                }
            }
        }
        [HarmonyPatch(typeof(GameData.Domains.SpecialEffect.CombatSkill.NoSect.FistAndPalm.TaiZuChangQuan),"OnEnable")]
        public static void Postfix(GameData.Domains.SpecialEffect.CombatSkill.NoSect.FistAndPalm.TaiZuChangQuan __instance, bool ___IsDirect) {
            new MetAncestor(__instance, ___IsDirect).Init();
        }
#endif
//        [HarmonyPostfix]
//        [HarmonyPatch(typeof(GameData.Domains.SpecialEffect.CombatSkill.NoSect.Agile.XiaoZongYueGong),"OnEnable")]
//        public static unsafe void XiaoZongYue_OnEnable(GameData.Domains.SpecialEffect.CombatSkill.NoSect.FistAndPalm.TaiZuChangQuan __instance, GameData.Common.DataContext context) {
//            for (int i = 0; i < 2; i++) {
//                DomainManager.Combat.AddTrick(context, GameData.Domains.DomainManager.Combat.GetElement_CombatCharacterDict(__instance.CharacterId), 6, true, true);
//            }
//            DomainManager.Combat.ShowSpecialEffectTips(context, __instance.CharacterId, RevEff_ShiXiangTieDanShou, 0);
//        }
        // [HarmonyPostfix, HarmonyPatch(typeof(GameData.Domains.SpecialEffect.CombatSkill.NoSect.FistAndPalm.TaiZuChangQuan),"OnEnable")]
        // public static unsafe void OnEnable(GameData.Domains.SpecialEffect.CombatSkill.NoSect.FistAndPalm.TaiZuChangQuan __instance, GameData.Common.DataContext context) {
        //     logger("启用太祖长拳: "+__instance.CharacterId);
        // }
        // [HarmonyPostfix, HarmonyPatch(typeof(GameData.Domains.SpecialEffect.CombatSkill.NoSect.FistAndPalm.TaiZuChangQuan),"OnDisable")]
        // public static unsafe void OnDisable(GameData.Domains.SpecialEffect.CombatSkill.NoSect.FistAndPalm.TaiZuChangQuan __instance, GameData.Common.DataContext context) {
        //     logger("禁用太祖长拳: "+__instance.CharacterId);
        // }
        //[HarmonyPatch(typeof(GameData.Domains.SpecialEffect.CombatSkill.NoSect.FistAndPalm.TaiZuChangQuan),"OnCombatStart")]
        // [HarmonyPatch(typeof(GameData.Domains.SpecialEffect.CombatSkill.CombatSkillEffectBase),"OnCombatStart")]
        // public static unsafe void OnPrepareSkillBegin(GameData.Domains.SpecialEffect.CombatSkill.NoSect.FistAndPalm.TaiZuChangQuan __instance, GameData.Common.DataContext context, int charId, bool isAlly, short skillId) {
        //     if (charId == __instance.CharacterId && skillId == __instance.SkillKey.SkillTemplateId) {
        //         for (int i = 0; i < 9; i++)
        //         {
        //             DomainManager.Combat.AddTrick(context, GameData.Domains.DomainManager.Combat.GetElement_CombatCharacterDict(__instance.CharacterId), 6, true, true);
        //         }
        //         DomainManager.Combat.ShowSpecialEffectTips(context, __instance.CharacterId, RevEff_ShiXiangTieDanShou, 0);
        //     }
        // }
    }
}
